<?php

class TemplateController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$templates = Template::paginate(10);
		return View::make('templates.index')->with(array('templates' => $templates));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$template = new Template;
		return View::make('templates.edit')->with(array('template' => $template, 'method' => 'post'));
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		// Get all the inputs
        // id is used for login, username is used for validation to return correct error-strings
        $userdata = array(
            'template_name' => Input::get('template_name'),
			'template_param_name' => Input::get('template_param_name')
        );
		

        // Declare the rules for the form validation.
        $rules = array(
            'template_name'  => 'Required|unique:templates,template_name',
			'template_param_name' => 'Required|min:1|array'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);
		
		// New template model
		$template = new Template;
		
        // Check if the form validates with success.
        if ($validator->passes()) {
		
			$template = new Template(array('template_name' => $userdata['template_name'], 'update_by' => Auth::User()->username, 'create_by' => Auth::User()->username));
			$template->save();
			
			foreach ($userdata['template_param_name'] as $templateParamName) {
				$templateParam = new TemplateParam(array('template_param_name' => $templateParamName, 'template_param_type' => 'STRING'));
				$template->templateParams()->save($templateParam);
			}
			
			if (isset($template->id))
			//if (Template::create($userdata))
			{
                // Redirect to homepage
                return Redirect::to('templates')->with('success', 'New template successfully added');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('templates/edit')->withErrors(array('message' => 'Opps something wrong!!'));
			}
		}
		
        // Something went wrong.	 
        return Redirect::to('templates/create')->with(array('method' => 'post'))->withErrors($validator)->withInput(Input::all());
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$template = Template::findOrFail($id);
		return View::make('templates.show')->with('template', $template);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$template = Template::findOrFail($id);
		return View::make('templates.edit')->with(array('template' => $template, 'method' => 'put'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		// Retrieve current user
		$template = Template::find($id);
	
		// Get all the inputs
        // id is used for login, username is used for validation to return correct error-strings
        $userdata = array(
            'template_name' => Input::get('template_name'),
			'template_param_name' => Input::get('template_param_name')
        );
		
        // Declare the rules for the form validation.
        $rules = array(
            'template_name'  => 'Required|unique:templates,template_name,'.$template->id,
			'template_param_name' => 'Required|min:1|array'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes()) {
			
			$template->template_name = $userdata['template_name'];
			$template->update_by = Auth::User()->username;
						
			if ($template->save())
			{
			
				$template->templateParams()->delete();
				
				foreach ($userdata['template_param_name'] as $templateParamName) {
					$templateParam = new TemplateParam(array('template_param_name' => $templateParamName, 'template_param_type' => 'STRING'));
					$template->templateParams()->save($templateParam);
				}
                // Redirect to homepage
                return Redirect::to('templates')->with('success', 'Template successfully Updated');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('templates.edit')->withErrors(array('message' => 'Opps something wrong!!'));
			}
		}
		
        // Something went wrong.
        return Redirect::to('templates/'.$template->id.'/edit')->with(array('template' => $template, 'method' => 'put'))
										 ->withErrors($validator);
	}
	
	/**
	 * Show the form for delete the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function delete($id)
	{
		$template = Template::find($id);
		return View::make('templates.edit')->with(array('template' => $template, 'method' => 'delete'));
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Retrieve current Department
		$template = Template::find($id);
		
		$template->teamplateParams()->sync(array());
		$template->delete();
		
		return Redirect::to('templates')->with('success', 'Successfully deleted template!');
	}


}
